// pages/activity/activity.js
import store from '../../store/index'
import { getActivity} from '../../api/activity'
import { error } from '../../lib/js/utils'
import { IMAGE_SERVER_URL } from '../../api/config'

Page(
  store.createPage({
    data:{
      actInfo: [],
      currentPage: 0
    },

    /**
     * 初始化数据
     */
    initData: function(){
      this.getNextDataActInfo()
      //console.log(getActivity(1,5))
    },

    /**
     * 获取下一页数据
     */
    getNextDataActInfo: function(){
      const that = this
      const currentPage = that.data.currentPage
      const nextPage = currentPage+1
      const actInfo = that.data.actInfo
      getActivity(nextPage).then(function(res){
        //console.log(res)
        if (res.data.rows.length === 0) {
          error('没有数据了')
          return
        }
        res.data.rows.forEach(item => {
          for(var i = 0;i < item.images.length;i++){
            item.images[i].url = `${IMAGE_SERVER_URL}/${item.images[i].url}`
          }
          //console.log(item)
          //console.log(actInfo)
          actInfo.push(item)
      })
      that.setData({
        currentPage: nextPage,
        actInfo: actInfo
      })
    })
},
    /**
     * 页面监听加载
    */
    onLoad: function(){
      this.initData()
    }
})

)
